package com.zxl.designpattern.creator.prototype;

/**
 * @author zxl
 * @version 1.0
 * @description 原型模式测试类
 * @date 2025/5/31 23:04
 */
public class Test {
    public static void main(String[] args) {

        try {
            WorkCardCache.loadCache();
            WorkCard prototype = WorkCardCache.getPrototype("workCard");
            System.err.println("原型对象：" + prototype);

//            testClone(prototype);
//            testManual(prototype);
            testIO(prototype);
//            testJson(prototype);

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
    public static void testClone(WorkCard prototype) throws CloneNotSupportedException{
        WorkCard workCard = WorkCardCache.getWorkCard("workCard", 0);
        System.out.println("浅复制：" + workCard);
        workCard.setCompName("浅公司");    //影响不到原型对象
        workCard.getEmployee().setName("zxl-浅"); //影响到原型对象
        System.out.println("修改后原型：" + prototype);
        System.out.println("浅复制：" + workCard);
    }
    public static void testManual(WorkCard prototype) throws CloneNotSupportedException{
        WorkCard workCard = WorkCardCache.getWorkCard("workCard", 1);
        System.out.println("复制的对象：" + workCard);
        workCard.setCompName("手动公司");   //影响不到原型对象
        workCard.getEmployee().setName("zxl-手动");   //影响不到原型对象
        System.out.println("手动修改后原型：" + prototype);
        System.out.println("手动复制：" + workCard);
    }
    public static void testIO(WorkCard prototype) throws CloneNotSupportedException{
        WorkCard workCard = WorkCardCache.getWorkCard("workCard", 2);
        System.out.println("复制的对象：" +workCard);
        workCard.setCompName("IO公司");   //影响不到原型对象
        workCard.getEmployee().setName("zxl-IO");   //影响不到原型对象
        System.out.println("IO修改后原型：" + prototype);
        System.out.println("IO复制：" + workCard);
    }
    public static void testJson(WorkCard prototype) throws CloneNotSupportedException{
        WorkCard workCard = WorkCardCache.getWorkCard("workCard", 3);
        System.out.println("复制的对象：" + workCard);
        workCard.setCompName("Json公司"); //影响不到原型对象
        workCard.getEmployee().setName("zxl-Json"); //影响不到原型对象
        System.out.println("Json修改后原型：" + prototype);
        System.out.println("Json复制：" + workCard);
    }
}
